package com.sc.syllabus.utils;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.sc.syllabus.R;
import com.sc.syllabus.view.CommonPopupWindow;

/**
 * Created by liushuanglong on 2017/9/21.
 */

public class PopupListUtils {

    private Activity mContext;
    private View activityPopup;
    private CommonPopupWindow window;
    private ListView dataList;
    private ArrayAdapter adapter;
    private String[] mData;
    private int layoutId;

    public PopupListUtils(Activity context, int layoutId, String[] data) {
        mContext = context;
        this.layoutId = layoutId;
        mData = data;
        initPopupWindow(data);
    }

    public void setSelect(int position) {
        dataList.smoothScrollToPosition(position);
        adapter.notifyDataSetChanged();
    }

    private void initPopupWindow(final String[] datas) {
        adapter = new ArrayAdapter<String>(mContext, R.layout.item_popup_list, datas);
        // get the height of screen
        DisplayMetrics metrics = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int screenHeight = metrics.heightPixels;
        // create popup window
        window = new CommonPopupWindow(mContext, R.layout.popup_list, ViewGroup.LayoutParams.MATCH_PARENT, (int) (screenHeight * 0.45)) {
            @Override
            protected void initView() {
                View view = getContentView();
                dataList = (ListView) view.findViewById(R.id.data_list);
                dataList.setAdapter(adapter);
            }

            @Override
            protected void initEvent() {
                dataList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        mListener.updatePosition(position);
                        mListener.updateTitle(mData[position]);
                        window.getPopupWindow().dismiss();
                    }
                });
            }

            @Override
            protected void initWindow() {
                super.initWindow();
                PopupWindow instance = getPopupWindow();
                instance.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
                        lp.alpha = 1.0f;
                        mContext.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                        mContext.getWindow().setAttributes(lp);
                    }
                });
            }
        };
    }

    public void show() {
        activityPopup = mContext.findViewById(layoutId);
        PopupWindow win = window.getPopupWindow();
        win.setAnimationStyle(R.style.animAlpha);
        win.showAtLocation(activityPopup, Gravity.BOTTOM, 0, 0);
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.3f;
        mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mContext.getWindow().setAttributes(lp);
    }

    private IPopupSelectListener mListener;

    public void registSelectListener(IPopupSelectListener listener) {
        this.mListener = listener;
    }

    public void unRegistSelectListener() {
        this.mListener = null;
    }
}
